﻿using MediatR;
using Product.API.Applcation.Command.ProductTypeInfo;
using Product.Domain;
using Product.Infrstructre;
using Product.Infrstructre.Impl;

namespace Product.API.Applcation.CommandHandlers.ProductTypeInfo
{
    /// <summary>
    /// 产品添加
    /// </summary>
    public class ProductCreateCommandHandler : IRequestHandler<ProductCreateCommand, int>
    {
        protected readonly IbaseProductRepository rep;

        public ProductCreateCommandHandler(IbaseProductRepository rep)
        {
            this.rep = rep;
        }
        /// <summary>
        /// 产品分类添加
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<int> Handle(ProductCreateCommand request, CancellationToken cancellationToken)
        {
            //判断是否存在同名分类
            var list = (await rep.GetAllAsync()).Count(x => x.ProductTypeName == request.ProductTypeName);
            if (list > 0)
            {
                //存在同名产品分类
                return -1;
            }
            else
            {
                //添加产品分类
                return await rep.AddAsync(new Domain.ProductType() { ProductTypeCode = request.ProductTypeCode, ProductTypeName = request.ProductTypeName, ParentId = request.ParentId, Premark = request.Premark, CreateTime = DateTime.Now, AddName = request.AddName, AddTime = DateTime.Now ,Office_Id = request.Office_Id });
            }

        }
    }
}
